/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import com.openbravo.data.loader.TableDBSentenceBuilder;
import e.odbo.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.I_ProductCategory;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import com.openbravo.pos.bean.ProductCategoryExt;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class ProductCategoryExtDAO extends ModifiedLogDAO<ProductCategoryExt>{

    public ProductCategoryExtDAO(I_Session s) {
        super(s);
    }


    @Override
    public TableDefinition getTable() {
        return new TableDefinition(
                "CATEGORIES",
                new Field[]{
                   new Field("ID",Datas.STRING, Formats.STRING),
                   new Field("NAME",Datas.STRING, Formats.STRING),
                   new Field("PARENTID",Datas.STRING, Formats.STRING,ProductCategoryExt.class),
                   new Field("IMAGE", Datas.IMAGE, Formats.NULL),
                   new Field("ISHOT",Datas.BOOLEAN, Formats.BOOLEAN),
                   new Field("LASTMODIFIED",Datas.TIMESTAMP, Formats.TIMESTAMP),
                   new Field(I_ProductCategory.CREATEDATE,Datas.TIMESTAMP, Formats.TIMESTAMP)
                } ,
                new int[]{0}){
					@Override
					public String getDefaultListOrder(String alise) {
						String ret=" order by ";
						if(alise!=null)
							ret+=alise+".";
						ret+=I_ProductCategory.CREATEDATE+" ASC ";
						return ret;
					}
        	
        };
    }

    @Override
    public void writeInsertValues(DataWrite dp, ProductCategoryExt obj) throws BasicException {
           dp.setString(1, obj.getID());
           dp.setString(2, obj.getName());
           dp.setString(3, obj.getM_sParentID());
           dp.setBytes(4, obj.getImage());
           dp.setBoolean(5, obj.isHot());
           dp.setTimestamp(6,obj.getLastModified());
           dp.setTimestamp(7,obj.getCreateDate());
    }

    public ProductCategoryExt readValues(DataRead dr,ProductCategoryExt obj) throws BasicException {
       if(obj==null)
    	   obj=new ProductCategoryExt();
       obj.setID(dr.getString(1));
       obj.setName(dr.getString(2));
       obj.setM_sParentID(dr.getString(3));
       obj.setImage(dr.getBytes(4));
       obj.setHot(dr.getBoolean(5));
       obj.setLastModified(dr.getTimestamp(6));
       obj.setCreateDate(dr.getTimestamp(7));
       return obj;
    }

    public final List<ProductCategoryExt> getRootProductCategories() throws BasicException {
        return new PreparedSentence(s, ((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE PARENTID IS NULL ORDER BY NAME", null, this).list();
    }

    public final List<ProductCategoryExt> getSubProductCategories(String category) throws BasicException {
        return new PreparedSentence(s, ((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE PARENTID = ? ORDER BY NAME", SerializerWriteString.INSTANCE, this).list(category);
    }

    //public final SentenceList getProductCategoriesList() {
       // return new StaticSentence(s, "SELECT ID, NAME, IMAGE, PARENTID, ISHOT FROM CATEGORIES ORDER BY NAME", null, this);
   // }

    public int getCatalogProductCategoryAdd(String categoryId) throws BasicException{
        return new StaticSentence(s, "INSERT INTO PRODUCTS_CAT(PRODUCT, CATORDER) SELECT ID, " + s.getDialect().INTEGER_NULL() + " FROM PRODUCTS WHERE CATEGORY = ?", SerializerWriteString.INSTANCE).exec(categoryId);
    }

    public int delCatalogProductCategory(String categoryId) throws BasicException {
        return new StaticSentence(s, "DELETE FROM PRODUCTS_CAT WHERE PRODUCT = ANY (SELECT ID FROM PRODUCTS WHERE CATEGORY = ?)", SerializerWriteString.INSTANCE).exec(categoryId);
    }

    @Override
    public Class getSuportClass() {
        return ProductCategoryExt.class;
    }

}
